<template>
	<view class="test-page">
		<web-view v-if="showWebview" id="webview_id" :src="src" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	import {
		getQuerSign
	} from '@/api/indexApprove.js'
	import {
		mapState,
		mapMutations
	} from 'vuex'
	// const SRC = 'http://172.16.21.96:5500/pageForm/index.html'
	const SRC = 'https://zhatu.deqing.gov.cn/createPdf/pageForm/index.html'
	export default {
		onLoad(options) {
			console.log(options.type)
			this.src = `${SRC}?type=${options.type}`
			this.webViewContext = my.createWebViewContext('webview_id');
			// 获取签章数据
			this.requestGetQuerSign()
		},
		mounted() {
			console.log('传过来的数据: ', this.infoApprove, this.infoOrder)
			this.showWebview = true
			this.timer = setInterval(() => {
				// 向webview发送信息
				this.webViewContext.postMessage({
					infoApprove: this.infoApprove,
					infoOrder: this.infoOrder,
					infoUser: this.userInfo
				});
			}, 200)
		},
		destroyed() {
			clearInterval(this.timer)
			this.timer = null
			clearInterval(this.recordTimer)
			this.recordTimer = null
		},
		computed: {
			...mapState(['userInfo']),
			...mapState('mdApprove', ['infoApprove']),
			...mapState('mdOrder', ['infoOrder', 'infoRecords']),
		},
		data() {
			return {
				src: '',
				webViewContext: null,
				showWebview: false,
				timer: null,
				recordTimer: null,
			}
		},
		methods: {
			...mapMutations('mdOrder', []),
			// 获取签章数据
			requestGetQuerSign() {
				return getQuerSign({
					ordersId: this.infoOrder.id
				}).then(res => {
					console.log('签章数据: ', res)
					this.recordTimer = setInterval(() => {
						// 向webview发送信息
						this.webViewContext.postMessage({
							type: 'log',
							infoRecords: res
						});
					}, 200)
				})
			},
			// webview向app发送信息
			handleMessage({
				detail
			}) {
				// uni.showModal({
				// 	title:detail
				// })
				console.log('收到webview信息', detail)

				uni.$emit('SetFinnishPdf', detail.data)
				uni.navigateBack()
				if (detail) {
					
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.test-page {
		width: 100%;
		height: 100%;

		#webview_id {
			width: 100%;
			height: 100%;
		}
	}
</style>
